<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2022/5/9
 * Time: 下午4:48
 */

namespace app\api\controller\v38;

use app\api\controller\v2\Auth;
use app\common\cachemodel\FortuneNote;
use app\common\cachemodel\lucky\AppLuckyTaskRecord;
use app\common\cachemodel\lucky\AppUserLuckyChangeRecord;
use app\common\cachemodel\lucky\AppUserLuckyInfo;
use app\common\lib\Util;
use think\Env;

/**
 * 幸运星改版
 * Class LuckyPlanet
 * @package app\api\controller\v32
 */
class LuckyPlanet extends Auth
{
    protected $starImg = 'https://oss.goddessxzns.com/test/lucky_star/star.png';

    /**
     * 幸运星首页
     */
    public function index()
    {
        $date = date('Ymd');
        $luckyTaskRecordModel = new AppLuckyTaskRecord();

        $luckyInfo = (new AppUserLuckyInfo())->IndexGetUserLuckyInfo($this->user['id']);

        $taskInfo = $luckyTaskRecordModel->getTaskList($this->user['id'], $this->user['rid'] ?? 0);
        // 获取幸运星 领取信息
        $userLuckyChangeRecordModel = new AppUserLuckyChangeRecord();
        // 获取明日即将过期
        $expireSoonNum = $luckyInfo['lucky_num'] > 0 ? $userLuckyChangeRecordModel->getExpireLuckyNumByTomorrow($this->user['id']) : 0;
        //获取今日心情签到信息
        $appMoodNoteModel = new FortuneNote();
        $todayNoteInfo = $appMoodNoteModel->checkTodayNoteStatus($this->user['id']);
        if ($todayNoteInfo)
        {
            $signBtn = [
                'icon' => $todayNoteInfo['mood_icon_s'],
                'title' => '记录完成明日再来',
                'target' => 'local',
                'link' => 'empty',
                'status' => 0,
            ];
        } else
        {
            $oss = Env::get('host.photo');
            $dateTmp = date('Y-m-d');
            $signBtn = [
                'icon' => $oss . '/test/mood/happy_s.png',
                'title' => '记录心情领幸运星x4',
                'target' => 'local',
                'link' => "mood_index?date={$dateTmp}&need_auth=2",
                'status' => 1,
            ];
        }
        if(Util::AppIsPassShowV1())
        {
            $otherData = [];
            foreach ($taskInfo['other'] as $item){
                if(in_array($item['id'],[205],true)){
                    $otherData[] = $item;
                }
            }
            $taskInfo['other'] = $otherData;
        }

        $data = [
            // 页面顶部数据
            'top_info' => [
                'have_num' => $luckyInfo['lucky_num'], // 当前拥有幸运星数量
                'expire_soon_num' => $expireSoonNum, // 明日即将摧毁的数量
                'introduce_title' => '什么是幸运星？幸运星又能做什么呢？', // 介绍的标题
                'target' => 'link', // 介绍的H5跳转地址
                'link' => Env::get('host.domain') . '/web/lucky_star/whatsnew.html', // 介绍的H5跳转地址
                'img' => $this->starImg, // 当前拥有23颗幸运星前面的那个小图片的地址
                'sign_btn' => $signBtn,
            ],
            // 任务数据
            'task_info' => [
                // 每日任务
                'everyday' => [
                    'name' => '每日任务',
                    'tips' => '每日凌晨0点刷新任务',
                    'list' => $taskInfo['everyday'],
                ],
                // 升级任务
                'upgrade' => [
                    'name' => '升级任务',
                    'tips' => '',
                    'list' => $taskInfo['upgrade'],
                ],
                // 其他任务
                'other' => [
                    'name' => '其他任务',
                    'tips' => '',
                    'list' => $taskInfo['other'],
                ],
            ],
            'first_info' => $luckyInfo['first_info'] ? ['compensation_text' => $luckyInfo['first_info']] : null
        ];

        return show(200, '请求成功', $data);
    }
}
